﻿Imports System.Reflection

Namespace Constants

    Public Module Consts

        Public Const EvoConfigIni As String = "evoconfig.ini"
        Public Const PatchConfigIni As String = "config.ini"
        Public Const PatcherExe As String = "KaoriPatchGZ120.exe"
        Public Const GameExe As String = "MirEvo.exe"
        Public Const ErrorFile As String = "error.txt"
    End Module

End Namespace

Namespace Globals

    Public Module GlobalVar

        Public sRootPath As String
        Public iniConfig As Config.IniFile

        Public ClientVersion As String

        Public StartForm As fStartup

        Public ResoloutionCount As Integer = 2
        Public aResouloutionX() As Integer = {800, 1024}
        Public aResouloutionY() As Integer = {600, 768}

        Public Sub Init(ByRef F As fStartup)

            StartForm = F

            Dim ExePath As String = Reflection.Assembly.GetExecutingAssembly().Location
            sRootPath = Left(ExePath, InStrRev(ExePath, "\"))

            If Not FileIO.FileSystem.FileExists(sRootPath & Constants.EvoConfigIni) Then
                FileIO.FileSystem.CopyFile(sRootPath & Constants.PatchConfigIni, sRootPath & Constants.EvoConfigIni)
            End If

            iniConfig = New Config.IniFile(sRootPath & Constants.EvoConfigIni)

            ClientVersion = GetClientVersion()

        End Sub

        Private Function GetClientVersion() As String

            Dim dllpath As String = (Globals.sRootPath & "EvoLib.Shared.dll")
            Dim A As Assembly = Assembly.LoadFile(dllpath)

            Dim T As System.Type = A.GetType("EvoLib.Protocol.mProtocol")
            Dim field As FieldInfo = T.GetField("sClientVersion")
            Return field.GetValue(T)

        End Function

    End Module

End Namespace
Module mMain

    Sub Main()

        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf ErrorHandling.UnhandledErrorHandler

        Dim bRunAutoPatch As Boolean = True
        If My.Application.CommandLineArgs.Contains("-noautopatch") Then bRunAutoPatch = False

        Dim ExePath As String = Reflection.Assembly.GetExecutingAssembly().Location
        ExePath = Left(ExePath, InStrRev(ExePath, "\"))

        Dim PatcherExePath As String = ExePath & Constants.PatcherExe
        Try
            Shell(PatcherExePath, AppWinStyle.NormalFocus, True)
        Catch ex As System.IO.FileNotFoundException
            MsgBox(Constants.PatcherExe & " is missing!", vbOKOnly OR MsgBoxstyle.Critical)
        End Try
        Dim F As New fStartup
        F.Show()
        F.Activate()
        Application.Run(F)

    End Sub

End Module
